C++ difference between "char *" and "char * = new char[]"

Posted by nashmaniac on Programmers See other posts from Programmers or by nashmaniac
Published on 2012-11-25T08:06:02Z Indexed on 2012/11/25 11:22 UTC
Read the original article Hit count: 351

Filed under:
|
|
|
|

So, if I want to declare an array of characters I can go this way

char a[2];
char * a ;
char * a = new char[2];

Ignoring the first declaration, the other two use pointers. As far as I know the third declaration is stored in heap and is freed using the delete operator . does the second declaration also hold the array in heap ? Does it mean that if something is stored in heap and not freed can be used anywhere in a file like a variable with file linkage ? I tried both third and second declaration in one function and then using the variable in another but it didn't work, why ? Are there any other differences between the second and third declarations ?

© Programmers or respective owner

Related posts about c++

Related posts about memory